Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### Using The ADO Data Control While the ADO data model is relatively simple and easy to use, the programmer’s task is made even easier by the ADO Data control. You had a brief look at this control in Chapter 20. Placed on a form, the ADO Data control serves as the datasource—the link between your program and the ADO interfaces. You program an ADO Data control by setting its properties and calling its methods. The control then takes care of the details of creating the required ADO interfaces. By binding other Visual Basic controls, such as Text Boxes, to the ADO Data control, the information is automatically transferred between the database and your program’s visual display. The ADO Data control works very much like the Data control used in Chapter 19’s Music database program, but the ADO Data control uses the newer ADO data model rather than the older DAO model. #### Accessing Databases Without The ADO Data Control In spite of the ADO Data control’s convenience, it does have limitations. For one, only a subset of Visual Basic controls can be bound to the ADO Data control. For another, use of Recordset objects is not as flexible when an ADO Data control is used. Chapter 20 presents some examples of accessing a database without using an ADO Data control. But even with its fairly minor limitations, using the ADO Data control undoubtedly simplifies your programming tasks. I make use of the ADO Data control whenever possible. After all, one of the main reasons I use Visual Basic is to take advantage of the many software components that it offers. If I wanted to do everything myself, I would program in C++. #### Creating Databases At Runtime So far, you have dealt only with situations where the database already exists on disk, having been initially created using the Visual Data Manager. An existing database created by any other method—with the Access or Paradox database programs, for example—is handled the same way. But what if the database doesn’t exist? Can you create one from scratch using Visual Basic? The answer is “yes, but…” Why the qualification? Indeed, you can create new databases in Visual Basic code, by defining the table and record structure, and then adding new data. In that sense, you can use Visual Basic to write a program that permits users to define their own databases from scratch, rather than limiting them to databases that already exist. Here comes the catch: I generally don’t recommend the approach just mentioned, because creating a database from scratch requires a lot of complicated and difficult programming. Not only do you have to accept user specifications regarding table structure, field names, data types, and the like—and convert this information into the necessary commands to create the database—but you also have to provide the capability to design forms and reports, if the data is to be of any value to the user. While this type of program can and has been written successfully in Visual Basic, writing such a program is not for the faint–hearted. Even if you find a custom control that provides much of the required functionality, the project is still daunting, with a lot of potential pitfalls. A much better approach is to use a dedicated database front–end generator, such as Visual Data Manager, Access, or Paradox, which have built–in functionality for creating new databases and designing forms. If you decide to take this approach and avoid Visual Basic altogether, your job as a programmer is to use the application’s internal programming language (Access Basic for Access, PAL for Paradox) to create the customized interfaces and queries that the client requires. Alternatively, after the database has been created, you can combine a commercial program (Access or Paradox) for database design and creation with a custom Visual Basic program for decision support and transaction processing. ### Data–Aware Controls Although data–aware controls were introduced earlier in the book, now is the time to look at the full complement. Data–aware controls work in conjunction with an ADO Data control. A data–aware control can be bound to a specific field in the data table for which an ADO Data control is serving as datasource. This binding is accomplished with the control’s DataSource property (set to the name of the ADO Data control) and its DataField property (set to the name of the desired field in the data table). Ten data–aware controls are provided with the Professional Edition of Visual Basic. The following is a brief review of Visual Basic’s data–aware controls, each of which can be bound to a single field in a data table: • Label—Used for static display of text data. The user cannot edit or otherwise modify data that is displayed in a Label control. • Text Box—Used for dynamic display of text data. The user can edit or enter data in a Text Box control. • Check Box—Used for dynamic display of Boolean (Yes/No, True/False) data. • Combo Box—The Text Box portion of the Combo Box displays the data in the linked field. The List Box portion contains items that have been added specifically with the AddItem method. If the user selects an item from the list, that item replaces the original field data. A Combo Box can be used for data entry/editing when the user must select from a predefined list of possibilities. • List Box—Similar to a Combo Box. • Picture Box—Can display a graphical image from a bitmap, icon, or metafile that is stored in a linked image/binary data field. • Image—Similar to a Picture Box, but uses fewer resources and offers less capability. • OLE—Serves as a container for an OLE object and can be bound to a data table. • MaskedEdit—Similar to a Text Box, but permits the definition of masks that control data input. • RichTextBox—Similar to a Text Box control, but provides additional formatting capabilities. As useful as these controls are, they are all limited to displaying data from one field in one record at a time. This is simply too limited for certain applications. Three custom data–aware controls can provide additional capabilities: DataGrid, DataList, and DataCombo. #### The DataGrid Control The DataGrid control provides a grid, or matrix, of cells. Each column in the grid displays data from one field in the linked data table, and each row displays data from one record. A DataGrid control is shown in Figure 21.3, displaying data from the Music database developed in an earlier chapter. By default, a DataGrid control displays all the table’s fields and records, with vertical and/or horizontal scrollbars if the data is too wide or too tall to display completely. The user can move from cell to cell, viewing and editing data. The bottom row on the control, marked by an asterisk in the left column, can be used to enter new data (this is not shown in Figure 21.3). Column widths can be changed, fonts can be specified, and many other aspects of the control’s appearance and behavior can be manipulated via control properties. Figure 21.3 The DataGrid control. The DataGrid control consists of a Columns collection, with one Column object for each column (field) in the control. A DataGrid control can contain a maximum of 32,767 columns; the number of rows is limited only by system resources. Each column is a partially independent object with its own properties. Thus, some properties belong to the DataGrid control itself and affect operation and appearance of the entire control, but each column also has its own set of properties. Each Column object has its own caption (header), font, and so on. The Columns collection has properties that relate to all columns, such as Count, which returns the number of columns in the control. The DataGrid control’s features are covered further in the upcoming demonstration program. Refer to the Visual Basic Help system for complete details on its properties, methods, and events. #### The DataList And DataCombo Controls The DataList and DataCombo controls are similar to the regular Combo Box and List Box controls: all four controls provide a list of items from which the user can choose; the difference lies in data binding ability. The DataList and DataCombo controls display a list of items, and the DataCombo adds a TextBox that displays the currently selected item, and permits entry/editing by the user. These controls differ from the standard Combo Box and List Box controls, however, because they are data–aware and can be automatically loaded with values from a datasource. The DataList and DataCombo controls can be extremely useful, to speed the task of data entry and to reduce the chance of errors. For example, when a new Invoice record is entered, the Customer and each Item likely come from existing tables in the database. Rather than require the operator to manually enter the customer name or the item’s stock number, you can display a DataList control for each field in the data table, showing all the currently available values for that entry. The following demonstration project shows how these controls are used in this manner. ### Designing The Database Application In the remainder of this chapter and in the next two chapters, I walk you through all the steps of planning and programming a relational database application by using Visual Basic’s ADO model. Before writing a single line of code, however, some planning is in order. How careful and thorough you are during the planning stages has a major impact on your speed and efficiency in completing the project—not to mention the quality of the finished product. The remainder of this chapter covers some general considerations related to database design, walking you through the first steps of designing the demonstration database program. The discussion is limited to designing a new database from scratch, whereby no existing tables or applications are considered. If your own project involves a legacy database, the design process is different. Although you probably won’t have the option of changing the table structure, the other considerations still apply.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.